package com.apache.funds.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.funds.entity.SvmUser;
import com.apache.funds.manager.SvmUserManager;
import com.apache.tools.StrUtil;

public class SvmUserManagerImpl implements SvmUserManager {

	protected IDao svmUserDao;
	protected final String entityName = "com.apache.funds.entity.SvmUser";

	public void setSvmUserDao(IDao svmUserDao) {
		this.svmUserDao = svmUserDao;
	}

	public String saveInfo(ParamsVo<SvmUser> vo) throws BusinessException {
		SvmUser svmUser = vo.getObj();
		String infoId = Validator.generate();
		if (StrUtil.isNotNull(svmUser.getUserId())) {
			infoId = svmUser.getUserId();
		}
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		//String cacheKey="svmUser_"+infoId;
		svmUser.setUserId(infoId);
		MethodParam param = new MethodParam("SvmUser", cacheKey, "", entityName);
		param.setVaule(svmUser);
		if (svmUserDao.insert(param)) {
			return infoId;
		}
		return "";
	}

	public boolean editInfo(ParamsVo<SvmUser> vo) throws BusinessException {
		SvmUser svmUser = vo.getObj();
		if (Validator.isNotNull(svmUser.getUserId())) {
			String cacheKey = "";
			//String cacheKey="svmUser_"+svmUser.getUserid();
			MethodParam param = new MethodParam("SvmUser", cacheKey, "", entityName);
			param.setVaule(svmUser);
			return svmUserDao.edit(param);
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<SvmUser> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "";
		//String cacheKey="svmUser_"+infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		SvmUser info = (SvmUser) svmUserDao.selectById(param);
		if (Validator.isEmpty(info)) {
			return false;
		}
		param.setVaule(info);//此处需要先将状态值赋值为删除状态
		if ("false".equals(mark)) {//逻辑删除
			param.setKey("SvmUser");
			return svmUserDao.edit(param);
		} else {
			param.setParams("userid", infoId);
			param.setDelete(true);
			return svmUserDao.delete(param);
		}
	}

	public Object getInfoById(ParamsVo<SvmUser> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="svmUser_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return svmUserDao.selectById(param);
	}

	public Object execute(ParamsVo<SvmUser> vo) {
		String key = vo.getMethodKey();
		if ("ByUserName".equals(key)) {
			MethodParam param = new MethodParam("ByUserName", "", "", entityName);
			param.setParams("userName", String.valueOf(vo.getParams("userName")));
			return svmUserDao.selectSingle(param);
		}
		if ("ByCreateUserEname".equals(key)) {
			MethodParam param = new MethodParam("ByCreateUserEname", "", "", entityName);
			param.setParams("createUserEname", String.valueOf(vo.getParams("createUserEname")));
			return svmUserDao.selectSingle(param);
		}
		return null;
	}

	public Page getPageInfo(ParamsVo<SvmUser> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		Page page = svmUserDao.pageSelect(param);
		return page;
	}

	public List<SvmUser> getList(ParamsVo<SvmUser> vo) {
		MethodParam param = setMethodParams(vo, 2);
		return svmUserDao.select(param);
	}

	public long countInfo(ParamsVo<SvmUser> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return svmUserDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<SvmUser> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(svmUserDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		SvmUser svmUser = vo.getObj();
		if (Validator.isNotNull(svmUser.getUserName())) {
			sb.append(" and userName = :userName");
			param.setParams("userName", svmUser.getUserName());
		}
		if (Validator.isNotNull(svmUser.getRealName())) {
			sb.append(" and realName = :realname");
			param.setParams("realName", svmUser.getRealName());
		}
		if (Validator.isNotNull(svmUser.getPassWord())) {
			sb.append(" and passWord = :passWord");
			param.setParams("passWord", svmUser.getPassWord());
		}
		if (Validator.isNotNull(svmUser.getStatus())) {
			sb.append(" and status = :status");
			param.setParams("status", svmUser.getStatus());
		}
		if (Validator.isNotNull(svmUser.getOrgId())) {
			sb.append(" and orgId = :orgId");
			param.setParams("orgId", svmUser.getOrgId());
		}
		if (Validator.isNotNull(svmUser.getOrgName())) {
			sb.append(" and orgName = :orgName");
			param.setParams("orgName", svmUser.getOrgName());
		}
		if (Validator.isNotNull(svmUser.getCreateTime())) {
			sb.append(" and createTime = :createTime");
			param.setParams("createTime", svmUser.getCreateTime());
		}
		if (Validator.isNotNull(svmUser.getCommScale())) {
			sb.append(" and commScale = :commScale");
			param.setParams("commScale", svmUser.getCommScale());
		}
		if (Validator.isNotNull(svmUser.getCertiFicate())) {
			sb.append(" and certiFicate = :certiFicate");
			param.setParams("certiFicate", svmUser.getCertiFicate());
		}
		if (Validator.isNotNull(svmUser.getMobile())) {
			sb.append(" and mobile = :mobile");
			param.setParams("mobile", svmUser.getMobile());
		}
		if (Validator.isNotNull(svmUser.getRole())) {
			sb.append(" and role = :role");
			param.setParams("role", svmUser.getRole());
		}
		if (Validator.isNotNull(svmUser.getCertiType())) {
			sb.append(" and certiType = :certiType");
			param.setParams("certiType", svmUser.getCertiType());
		}
		if (Validator.isNotNull(svmUser.getEmail())) {
			sb.append(" and email = :email");
			param.setParams("email", svmUser.getEmail());
		}
		if (Validator.isNotNull(svmUser.getFax())) {
			sb.append(" and fax = :fax");
			param.setParams("fax", svmUser.getFax());
		}
		if (Validator.isNotNull(svmUser.getPhone())) {
			sb.append(" and phone = :phone");
			param.setParams("phone", svmUser.getPhone());
		}
		if (Validator.isNotNull(svmUser.getZipCode())) {
			sb.append(" and zipCode = :zipCode");
			param.setParams("zipCode", svmUser.getZipCode());
		}
		if (Validator.isNotNull(svmUser.getContactAddress())) {
			sb.append(" and contactAddress = :contactAddress");
			param.setParams("contactAddress", svmUser.getContactAddress());
		}
		if (Validator.isNotNull(svmUser.getBankType())) {
			sb.append(" and bankType = :bankType");
			param.setParams("bankType", svmUser.getBankType());
		}
		if (Validator.isNotNull(svmUser.getBankAccount())) {
			sb.append(" and bankAccount = :bankAccount");
			param.setParams("bankAccount", svmUser.getBankAccount());
		}
		if (Validator.isNotNull(svmUser.getBound())) {
			sb.append(" and bound = :bound");
			param.setParams("bound", svmUser.getBound());
		}
		if (Validator.isNotNull(svmUser.getSortIndex())) {
			sb.append(" and sortIndex = :sortIndex");
			param.setParams("sortIndex", svmUser.getSortIndex());
		}
		if (Validator.isNotNull(svmUser.getWebType())) {
			sb.append(" and webType = :webType");
			param.setParams("webType", svmUser.getWebType());
		}
		if (Validator.isNotNull(svmUser.getAid())) {
			sb.append(" and aid = :aid");
			param.setParams("aid", svmUser.getAid());
		}
		if (Validator.isNotNull(svmUser.getContactUser())) {
			sb.append(" and contactuser = :contactuser");
			param.setParams("contacTuser", svmUser.getContactUser());
		}
		if (Validator.isNotNull(svmUser.getIsTrans())) {
			sb.append(" and isTrans = :isTrans");
			param.setParams("isTrans", svmUser.getIsTrans());
		}
		if (Validator.isNotNull(svmUser.getOrgType())) {
			sb.append(" and orgType = :orgType");
			param.setParams("orgType", svmUser.getOrgType());
		}
		if (Validator.isNotNull(svmUser.getSpare1())) {
			sb.append(" and spare1 = :spare1");
			param.setParams("spare1", svmUser.getSpare1());
		}
		if (Validator.isNotNull(svmUser.getSpare2())) {
			sb.append(" and spare2 = :spare2");
			param.setParams("spare2", svmUser.getSpare2());
		}
		if (Validator.isNotNull(svmUser.getSpare3())) {
			sb.append(" and spare3 = :spare3");
			param.setParams("spare3", svmUser.getSpare3());
		}
		if (Validator.isNotNull(svmUser.getOnBalance())) {
			sb.append(" and onBalance = :onBalance");
			param.setParams("onBalance", svmUser.getOnBalance());
		}
		if (Validator.isNotNull(svmUser.getBid())) {
			sb.append(" and bid = :bid");
			param.setParams("bid", svmUser.getBid());
		}
		if (Validator.isNotNull(svmUser.getUserId())) {
			sb.append(" and userId = :userId");
			param.setParams("userId", svmUser.getUserId());
		}
		if (Validator.isNotNull(svmUser.getCreateUser())) {
			sb.append(" and createUser = :createUser");
			param.setParams("createUser", svmUser.getCreateUser());
		}
		if (Validator.isNotNull(svmUser.getIsUkey())) {
			sb.append(" and isUkey = :isUkey");
			param.setParams("isUkey", svmUser.getIsUkey());
		}
		if (Validator.isNotNull(svmUser.getCreateUserName())) {
			sb.append(" and createUserName = :createUserName");
			param.setParams("createUserName", svmUser.getCreateUserName());
		}
		param.setSqlStr(sb.toString());
		return param;
	}
}
